<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
      <el-form-item label="登录账号" prop="userName">
        <el-input v-model="queryParams.userName" clearable placeholder="请输入登录账号" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-has-permi="['monitor:online:batchLogout']"
          type="danger"
          size="mini"
          icon="el-icon-delete"
          plain
          :disabled="multiple"
          @click="handleForceLogoutAll"
        >强退所有设备</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="onlineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNumber - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="账号" align="center" prop="userName" />
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="部门" align="center" prop="deptName" />
      <el-table-column label="登录设备数" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.devices.length }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-has-permi="['monitor:online:query']"
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >详细</el-button>
          <el-button
            v-has-permi="['monitor:online:batchLogout']"
            style="color: red;"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleForceLogoutAll(scope.row)"
          >强退所有设备</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNumber"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 登录设备详情对话框 -->
    <el-dialog title="登录设备详情" :visible.sync="open" width="600px" append-to-body :close-on-click-modal="false">
      <el-descriptions direction="vertical" border>
        <el-descriptions-item label="账号">{{ form.userName }}</el-descriptions-item>
        <el-descriptions-item label="昵称">{{ form.nickName }}</el-descriptions-item>
        <el-descriptions-item label="部门">{{ form.deptName }}</el-descriptions-item>
      </el-descriptions>
      <el-table :data="form.devices" style="margin-top: 30px;">
        <el-table-column label="设备" align="center" prop="deviceType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.login_device_type" :value="scope.row.deviceType" />
          </template>
        </el-table-column>
        <el-table-column label="登录IP地址" align="center" prop="loginIp" />
        <el-table-column label="登录时间" align="center" prop="loginTime" width="180">
          <template slot-scope="scope">
            <span>{{ $parseTime(scope.row.loginTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              v-has-permi="['monitor:online:forceLogout']"
              style="color: red;"
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleForceLogout(scope.row)"
            >强退</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open=false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOnline, forceLogoutAll, forceLogout } from '@/api/monitor/online'

export default {
  name: 'Online',
  dicts: ['login_device_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      onlineList: [],
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNumber: 1,
        pageSize: 10,
        userName: undefined
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询在线用户列表 */
    getList() {
      this.loading = true
      listOnline(this.queryParams).then(res => {
        this.onlineList = res.list
        this.total = Number(res.total)
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNumber = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userid)
      this.multiple = !selection.length
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.$resetForm('form')
      this.open = true
      this.form = row
    },
    /** 强退所有按钮操作 */
    handleForceLogoutAll(row) {
      const userIds = row.userid || this.ids
      this.$modal.confirm('是否确认强退这 ' + userIds.length + ' 个用户的所有登录设备?').then(function() {
        return forceLogoutAll(userIds)
      }).then(() => {
        this.getList()
        this.open = false
        this.$modal.msgSuccess('强退成功')
      }).catch(() => {})
    },
    /** 强退按钮操作 */
    handleForceLogout(row) {
      this.$modal.confirm('是否确认强退账号为 "' + this.form.userName + '" 的用户?').then(function() {
        return forceLogout(row.token)
      }).then(() => {
        this.form.devices = this.form.devices.filter(item => item.token !== row.token)
        if (this.form.devices.length === 0) {
          this.getList()
          this.open = false
        }
        this.$modal.msgSuccess('强退成功')
      }).catch(() => {})
    }
  }
}
</script>
